{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch.nn as nn\n",
    "import torch\n",
    "\n",
    "\n",
    "class SE_VGG(nn.Module):\n",
    "    def __init__(self, num_classes):\n",
    "        super().__init__()\n",
    "        self.num_classes = num_classes\n",
    "        # define an empty for Conv_ReLU_MaxPool\n",
    "        net = []\n",
    "\n",
    "        # block 1\n",
    "        net.append(nn.Conv2d(in_channels=3, out_channels=64, padding=1, kernel_size=3, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=64, out_channels=64, padding=1, kernel_size=3, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n",
    "\n",
    "        # block 2\n",
    "        net.append(nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n",
    "\n",
    "        # block 3\n",
    "        net.append(nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n",
    "\n",
    "        # block 4\n",
    "        net.append(nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n",
    "\n",
    "        # block 5\n",
    "        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1, stride=1))\n",
    "        net.append(nn.ReLU())\n",
    "        net.append(nn.MaxPool2d(kernel_size=2, stride=2))\n",
    "\n",
    "        # add net into class property\n",
    "        self.extract_feature = nn.Sequential(*net)\n",
    "\n",
    "        # define an empty container for Linear operations\n",
    "        classifier = []\n",
    "        classifier.append(nn.Linear(in_features=512*7*7, out_features=4096))\n",
    "        classifier.append(nn.ReLU())\n",
    "        classifier.append(nn.Dropout(p=0.5))\n",
    "        classifier.append(nn.Linear(in_features=4096, out_features=4096))\n",
    "        classifier.append(nn.ReLU())\n",
    "        classifier.append(nn.Dropout(p=0.5))\n",
    "        classifier.append(nn.Linear(in_features=4096, out_features=self.num_classes))\n",
    "\n",
    "        # add classifier into class property\n",
    "        self.classifier = nn.Sequential(*classifier)\n",
    "\n",
    "\n",
    "    def forward(self, x):\n",
    "        feature = self.extract_feature(x)\n",
    "        feature = feature.view(x.size(0), -1)\n",
    "        classify_result = self.classifier(feature)\n",
    "        return classify_result\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    x = torch.rand(size=(8, 3, 224, 224))\n",
    "    vgg = SE_VGG(num_classes=1000)\n",
    "    out = vgg(x)\n",
    "    print(out.size())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}